package com.gjr.demo;

import com.gjr.eduservice.EduApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.lang.model.element.VariableElement;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 呙境仁
 * @create 2021-09-21 8:38
 */
@SpringBootTest(classes = EduApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class NewDateDemo {

    /**
     * 获取时间
     */
    @Test
    public void test() {
        System.out.println("LocalDate.now 年-月-日：" + LocalDate.now());
        System.out.println("LocalTime.now 小时-分钟-秒：" + LocalTime.now());
        System.out.println("年-月-日T小时-分钟-秒：" + LocalDateTime.now());
    }

    /**
     * 修改时间
     */
    @Test
    public void test1() {
        LocalDateTime now = LocalDateTime.now();
        // 修改后返回新的时间对象
        LocalDateTime dateTime = now.withYear(2001);
        System.out.println("dateTime = " + dateTime);
        System.out.println("now == dateTime：" + (now == dateTime));

        // 增加或减去时间
        // plus：增加指定的时间
        // minus：减去指定的时间
        System.out.println(dateTime.plusYears(20));
        System.out.println(dateTime.minusDays(20L));
    }

    /**
     * 比较时间
     */
    @Test
    public void test2() {
        LocalDateTime dateTime = LocalDateTime.of(2019, 9, 01, 20, 11, 11);

        LocalDateTime now = LocalDateTime.now();

        // 当前时间是否在指定时间之前
        System.out.println(now.isBefore(dateTime));
        // 当前时间是否在指定时间之后
        System.out.println(now.isAfter(dateTime));
        // 两个时间是否相等
        System.out.println(now.isEqual(dateTime));
    }

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Test
    public void test3() throws ParseException {
        System.out.println(daysBetween(1));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,7);

        System.out.println(calendar.getTimeInMillis());
    }


    /**
     * 计算两个日期之间相差的天数
     *
     * @param monthNum 月数
     * @return 相差天数
     * @throws ParseException 时间转换时可能发生的异常
     */
    public static int daysBetween(int monthNum) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date = sdf.parse(sdf.format(date));

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        long time1 = cal1.getTimeInMillis();

        Calendar cal2 = Calendar.getInstance();
        cal2.add(Calendar.MONTH, monthNum);
        long time2 = cal2.getTimeInMillis();

        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }
}
